package util;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class StartDialog extends JDialog {
	
	private String server_adr = "localhost";
	private int port_no = 8000;
	
	private boolean close = false;
	private JDialog d = new JDialog(this, "SCADA Client", true);
	private JTextField tf_portNo = new JTextField(""+port_no, 5);
	private JTextField tf_serverAdr = new JTextField(server_adr,30);
	
	public StartDialog()
	{
		d.getContentPane().setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.weightx = 0;
		gbc.weighty = 100;
		gbc.fill = GridBagConstraints.NONE;
		gbc.anchor = GridBagConstraints.EAST;
		add(new JLabel("Server IP"), gbc, 0, 0, 1, 1);
		add(new JLabel("Port number"), gbc, 0, 1, 1, 1);
		gbc.weightx = 100;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.anchor = GridBagConstraints.WEST;
		add(tf_serverAdr, gbc, 1, 0, 1, 1);
		add(tf_portNo, gbc, 1, 1, 1, 1);
		gbc.fill = GridBagConstraints.NONE;
		gbc.anchor = GridBagConstraints.CENTER;
		JButton loadButton = new JButton("Start");
		add(loadButton, gbc, 0, 2, 1, 1);
		JButton closeButton = new JButton("Close");
		add(closeButton, gbc, 1, 2, 1, 1);
		d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		loadButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				
				server_adr = tf_serverAdr.getText();
				port_no = Integer.parseInt(tf_portNo.getText());
				
				d.dispose();

			}
		});
		
		closeButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				close = true;
				d.dispose();
			}
		});
	}
	
	private void add(Component c, GridBagConstraints gbc,
			int x, int y, int w, int h)
	{
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = w;
		gbc.gridheight = h;
		d.getContentPane().add(c, gbc);
	}

	public void showDialog()
	{
		d.pack();
		d.setVisible(true);
	}

	public String getServerAdr()
	{
		return server_adr;
	}
	
	public int getPortNo()
	{
		return port_no;
	}
	
	public boolean isClosePressed()
	{
		return close;
	}
}
